package com.visikard.ui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.item.ItemAudienceKard;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class ListUserRatingAdapter  extends BaseAdapter {

	Context context;
	ArrayList<ItemAudienceKard> data;
	ArrayList<ItemAudienceKard> dataAll;
	LayoutInflater inflater;
	DisplayMetrics metrics;

	public ListUserRatingAdapter(Context ct, ArrayList<ItemAudienceKard> dt) {
		this.context = ct;
		this.data = dt;
		this.dataAll = dt;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.layout_item_kard_audience, null);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();

			viewHolder.imgKard = (ImageView) convertView.findViewById(R.id.image_kard);
			viewHolder.txtKardName = (TextView) convertView.findViewById(R.id.kard_name);
			viewHolder.txtDetails = (TextView) convertView.findViewById(R.id.text_detail_kard);
			viewHolder.groupRating = (ViewGroup) convertView.findViewById(R.id.group_rating_view);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final ItemAudienceKard item = (ItemAudienceKard) getItem(position);
		if (item != null) {

			if (item.getKardName() != null && item.getKardName().length() > 0)
				viewHolder.txtKardName.setText(item.getKardName());
			else
				viewHolder.txtKardName.setText("");
			
			if(item.getComments() != null && item.getComments().length() > 0)
				viewHolder.txtDetails.setText(item.getComments());
			else
				viewHolder.txtDetails.setText("");
			
			int mWidth = metrics.widthPixels / 4;
			int mHeight = 3 * mWidth / 2;

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.imgKard
					.getLayoutParams();

			lyKard.width = mWidth;
			lyKard.height = mHeight;

//			ImageLoader.getInstance(context).loadImageCorner(viewHolder.imgKard, item.getImageSmall(),
//					Device.getDeviceWidth() / 3, BitmapUtils.ROUND_ALL, 4, 3, R.drawable.kard_shadow_default_vertical, 0, 0);
			UILImageUtils.loadImage(viewHolder.imgKard, //image view
					item.getImageSmall(), //url
					1, //size of view
					BitmapUtils.ROUND_ALL, //round all 
					4, // round size
					3, //shadow
					R.drawable.kard_shadow_default_vertical, //shadow resource 
					0, 0); 
			
			if(item.getRating() > 0){
				// show rating bar small
				for (int i = 0; i < viewHolder.groupRating.getChildCount(); i++) {
					if (i <= (item.getRating() - 1)) {
						((ImageView) viewHolder.groupRating.getChildAt(i))
								.setImageResource(R.drawable.star_active);
					} else {
						((ImageView) viewHolder.groupRating.getChildAt(i))
								.setImageResource(R.drawable.star);
					}
				}
			}
		}
		return convertView;
	}

	private class ViewHolder {
		TextView txtKardName, txtDetails;
		ImageView imgKard;
		ViewGroup groupRating;
	}
}
